//
//  UserSayCell.m
//  danpin
//
//  Created by chuangjia on 29/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "UserSayCell.h"

#import "UILabel+StringFrame.h"
@interface UserSayCell()
{
    
}
@property(nonatomic,strong)UIImageView *icon;
@property(nonatomic,strong)UILabel* name;
@property(nonatomic,strong)UILabel *time;
@property(nonatomic,strong)UILabel *action;
@property(nonatomic,strong)UIImageView *images;
@property(nonatomic,strong)UIView *content;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)UIImageView *lineView;
@end

@implementation UserSayCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer* singleRecognizer1= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFromUser)];
        _icon =[[UIImageView alloc] initWithFrame:CGRectZero];
        _icon.layer.cornerRadius=PHOTO_FRAME_WIDTH*1.5;
        _icon.layer.masksToBounds = YES;
        _icon.userInteractionEnabled=YES;
        [_icon addGestureRecognizer:singleRecognizer1];
        [self.contentView addSubview:_icon];
        singleRecognizer1=nil;
        
        //
        _name =[[UILabel alloc] initWithFrame:CGRectZero];
        _name.backgroundColor = [UIColor clearColor];
        _name.textColor = UIColorFromRGB(0x808080);
        _name.textAlignment = NSTextAlignmentLeft;
        _name.font = FONT(14);
        _name.numberOfLines=0;
        [self.contentView addSubview:_name];
        //
        _action =[[UILabel alloc] initWithFrame:CGRectZero];
        _action.backgroundColor = [UIColor clearColor];
        _action.textColor = UIColorFromRGB(0xbfbfbf);
        _action.textAlignment = NSTextAlignmentLeft;
        _action.font = FONT(14);
        _action.numberOfLines=0;
        [self.contentView addSubview:_action];
        //
        _time =[[UILabel alloc] initWithFrame:CGRectZero];
        _time.backgroundColor = [UIColor clearColor];
        _time.textColor = UIColorFromRGB(0xcccccc);
        _time.textAlignment = NSTextAlignmentLeft;
        _time.font = FONT(10);
        _time.numberOfLines=0;
        [self.contentView addSubview:_time];
        //images
        _images =[[UIImageView alloc] initWithFrame:CGRectZero];
        _images.contentMode=UIViewContentModeScaleToFill;
        [self.contentView addSubview:_images];
        //
        _content =[[UIView alloc] initWithFrame:CGRectZero];
        _content.backgroundColor=UIColorFromRGB(0xf5f5f5);
        [self.contentView addSubview:_content];
        
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*8,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = UIColorFromRGB(0x808080);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT(14);
        _titleLabel.lineSpacing =LSpacing;
        [_content addSubview:_titleLabel];
        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineView];
        
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.other=nil;
    self.indexPath=nil;
    
    self.icon=nil;
    self.name=nil;
    self.action=nil;
    self.time =nil;
    [self.content removeFromSuperview];
    self.content=nil;
    self.titleLabel=nil;
    [self.lineView removeFromSuperview];
    self.lineView =nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.lineView.frame =CGRectMake(PHOTO_TIME_PADDING,self.bounds.size.height-0.5, DEVICE_WIDTH-PHOTO_TIME_PADDING*2, 0.5);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    
    CGFloat fTop = PHOTO_FRAME_WIDTH;
    //user
    id send_user =[dict objectForKey:@"user"];
    NSString *user_name = @"";
    if (send_user && [send_user isKindOfClass:[NSDictionary class]]) {
        _icon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*3, PHOTO_FRAME_WIDTH*3);
        NSString * strURL =[[NSString stringWithFormat:@"%@", [send_user objectForKey:@"largeFace"]] replaceNullString];
        [_icon sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
        
        user_name = [[NSString stringWithFormat:@"%@", [send_user objectForKey:@"nick_name"]] replaceNullString];
        fTop = PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING+PHOTO_FRAME_WIDTH*3;
    }
    //name
    if (user_name.length>0) {
        _name.text = user_name;
        CGSize sizename =[_name boundingRectWithSize:CGSizeMake(DEVICE_WIDTH, PHOTO_FRAME_WIDTH*2)];
        _name.frame=CGRectMake( _icon.frame.origin.x+ _icon.frame.size.width+PHOTO_TIME_PADDING+1,_icon.frame.origin.y, sizename.width, PHOTO_FRAME_WIDTH*2);
    }
    //action
    //
    NSString *action = @"评论了";
    if (action.length>0) {
        _action.text =action;
        CGSize sizeaction =[_action boundingRectWithSize:CGSizeMake(DEVICE_WIDTH, PHOTO_FRAME_WIDTH*2)];
        _action.frame=CGRectMake( _name.frame.origin.x+ _name.frame.size.width+PHOTO_TIME_PADDING,_name.frame.origin.y, sizeaction.width, PHOTO_FRAME_WIDTH*2);
    }
    //time
    NSString *msg_date = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"comment_dateinfo"]] replaceNullString];
    if (msg_date.length>0) {
        _time.text =msg_date;
        CGSize sizetime =[_time boundingRectWithSize:CGSizeMake(DEVICE_WIDTH, PHOTO_FRAME_WIDTH*2)];
        _time.frame=CGRectMake(_name.frame.origin.x,_name.frame.origin.y+PHOTO_FRAME_WIDTH*1.5, sizetime.width, PHOTO_FRAME_WIDTH*2);
    }
    
    id ware =[dict objectForKey:@"ware"];
    if (ware && [ware isKindOfClass:[NSDictionary class]]) {
        NSString * strURL =[[NSString stringWithFormat:@"%@", [ware objectForKey:@"largePicURL"]] replaceNullString];
        if (strURL.length>0) {
            _images.frame =CGRectMake(DEVICE_WIDTH-PHOTO_FRAME_WIDTH*5,PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
            [_images sd_setImageWithURL:[NSURL URLWithString:strURL] placeholderImage:[UIImage imageNamed:@"image.png"]];
            fTop = PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING+PHOTO_FRAME_WIDTH*4;
        }
        
    }
    //time
    NSString *msg_info = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"text"]] replaceNullString];
    if (msg_info.length>0) {
        
        [_titleLabel setText:msg_info];
        CGSize optimumSize = [_titleLabel optimumSize];
        
        _content.frame =CGRectMake(_icon.frame.origin.x+ _icon.frame.size.width+PHOTO_TIME_PADDING,fTop+PHOTO_FRAME_WIDTH, DEVICE_WIDTH-(_icon.frame.origin.x+ _icon.frame.size.width+PHOTO_TIME_PADDING)-PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*2+optimumSize.height);
        _titleLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH,_content.frame.size.width - PHOTO_FRAME_WIDTH*2, optimumSize.height);
        
    }
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}

/// 返回Cell高度/* msg_type 0-评论 1-喜欢 2-关注 3-取消关注 4-@你说 5-私信你说 6-删除评论 11-推荐榜单 12-顶了单品

+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary *)model;
    CGFloat hight=0;
    id send_user =[dict objectForKey:@"user"];
    NSString *user_name = @"";
    if (send_user && [send_user isKindOfClass:[NSDictionary class]]) {
        user_name = [[NSString stringWithFormat:@"%@", [send_user objectForKey:@"nick_name"]] replaceNullString];
        hight = PHOTO_FRAME_WIDTH*3+(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING);
    }
    id ware =[dict objectForKey:@"ware"];
    if (ware && [ware isKindOfClass:[NSDictionary class]]) {
        NSString * strURL =[[NSString stringWithFormat:@"%@", [ware objectForKey:@"largePicURL"]] replaceNullString];
        if (strURL.length>0) {
            hight = PHOTO_FRAME_WIDTH*4+(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING);
        }
    }
    //
    NSString *msg_info = [[NSString stringWithFormat:@"%@",[dict objectForKey:@"text"]] replaceNullString];
    if (msg_info.length>0) {
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*8,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = LSpacing;
        [textLabel setText:msg_info];
        CGSize optimumSize1 = [textLabel optimumSize];
        textLabel=nil;
        hight+=optimumSize1.height+PHOTO_FRAME_WIDTH*3;
    }
    hight+=(PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING);
    return hight;
}

-(void)handleSingleTapFromUser
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_sunUser,nil,self.data,self.key,self.indexPath);
    }
}
@end
